#!/bin/bash
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/sys_check.sh"
echo "lmdprogress:3"
source ${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_conda_pytorch.sh
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/conda_run.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/conda_and_pip_settings.sh"
source "$LMD_BASE_INSTALL_SCRIPT_DIR/apps/f5-tts/env"

export CURRENT_APP_PATH="${LMD_APPS_DIR}/f5-tts"
echo $CURRENT_APP_PATH
mkdir -p "$CURRENT_APP_PATH"

echo "lmdprogress:15"

if [ -d "$CURRENT_APP_PATH" ]; then
    echo "App Dir: "$CURRENT_APP_PATH
else
    echo "Error, App Dir dose not exist, please retry. "$CURRENT_APP_PATH
    exit 1
fi

cd "$CURRENT_APP_PATH"
CONDA_PREFIX="./venv"
if [ -d "$CONDA_PREFIX" ]; then
    echo "found ./venv. remove it. "
    rm -rf "$CONDA_PREFIX"
fi
export PYTHON_VER_ENV=3.12
conda_create_env $CONDA_PREFIX python=$PYTHON_VER_ENV pip=25.2 -y

conda run -p $CONDA_PREFIX --no-capture-output python -m ensurepip --upgrade
conda_run_pip_install --upgrade setuptools

echo "lmdprogress:25"


export GIT_HOST=${GITHUB_PROXY}"https://github.com"

export GIT_REPO_URL=${GIT_HOST}/SWivid/F5-TTS.git
export GIT_REPO_BRANCH="main"
export LOCAL_REPO_PATH="$CURRENT_APP_PATH"
sh "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/git_clone_and_fetch.sh"

echo "lmdprogress:40"


function install_torch {
  if [ -f /System/Library/CoreServices/SystemVersion.plist ]; then
    # conda_run_pip_install --pre torch torchaudio --extra-index-url https://download.pytorch.org/whl/nightly/cpu
    source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_torch_mac.sh"
  elif current_os_is_windows; then
    # source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_torch_win.sh"
    source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_torch_multi_method.sh"
  fi
}

install_torch


echo "lmdprogress:60"

conda_run_pip_install "$CURRENT_APP_PATH"

echo "lmdprogress:80"

# conda run -p $CONDA_PREFIX --no-capture-output conda install -c conda-forge ffmpeg -y

function install_ffmpeg {
  source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_ffmpeg.sh"
}

function check_ffmpeg_and_install_win {
  PROGRAMS=("ffmpeg" "ffprobe")
  missing_programs=0

  for PROGRAM in "${PROGRAMS[@]}"; do
    if ! command -v ${PROGRAM} >/dev/null 2>&1; then
      $PROGRAM -v
      echo "$PROGRAM not found"
      missing_programs=1
    fi
  done

  if [ $missing_programs -eq 1 ]; then
    echo "need install ffmpeg and ffprobe。"
    install_ffmpeg
  else
    echo "ffmpeg and ffprobe exist"
  fi
}

function check_ffmpeg_and_install {
    if [ -f /System/Library/CoreServices/SystemVersion.plist ]; then
        if conda run -p $CONDA_PREFIX --no-capture-output conda list ffmpeg | grep -q "ffmpeg"; then
            echo "found ffmpeg"
        else
            echo "not found ffmpeg. install it."
            conda_run_conda_forge_install ffmpeg
        fi
    elif current_os_is_windows; then
        check_ffmpeg_and_install_win
    fi
}

check_ffmpeg_and_install

echo "lmdprogress:90"

if [ -n "$_INSTALL_SUCCESS_LOG" ]; then
    echo $_INSTALL_SUCCESS_LOG
fi

echo "lmdprogress:100"

